import { layer } from  "@layui/layui-vue"

const baseNotifyRT = function (data) {
    layer.notify({
        title: data.title,
        content: data.content,
        offset: data.type // 通知位置rb 右上 lt 
    });
};


const baseNotifyInfo=function(data){
    layer.notify({
      title: data.title, // Success Error Warming Question
      content: data.content,
      icon: data.type // 1成功 2失败 3警告 4锁定
    });
}

const getTimeY_d_m = () => {
    const now = new Date();
    const year = now.getFullYear();
    const month = String(now.getMonth() + 1).padStart(2, '0');
    const day = String(now.getDate()).padStart(2, '0');
    return `${year}-${month}-${day}`;
};

const updateTime = () => {
    const now = new Date();
    const year = now.getFullYear();
    const month = String(now.getMonth() + 1).padStart(2, '0');
    const day = String(now.getDate()).padStart(2, '0');
    const hours = String(now.getHours()).padStart(2, '0');
    const minutes = String(now.getMinutes()).padStart(2, '0');
    const seconds = String(now.getSeconds()).padStart(2, '0');

    return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
};

// 更新文件名截断函数，保留扩展名
const truncateFileName = (name, maxLength = 15) => {
    if (!name) return '未知文件';
    
    // 检测是否为移动设备
    const isMobile = window.innerWidth <= 768;
    
    // 如果不是移动设备，直接返回原文件名
    if (!isMobile) {
        return name;
    }
    
    if (name.length <= maxLength) {
        return name;
    }

    const lastDotIndex = name.lastIndexOf('.');
    // 如果没有扩展名或点在开头，则简单截断
    if (lastDotIndex <= 0) {
        return name.slice(0, maxLength - 3) + '...';
    }

    const baseName = name.slice(0, lastDotIndex);
    const extension = name.slice(lastDotIndex); // 包括 '.'

    const ellipsis = '...';
    const maxBaseLength = maxLength - extension.length - ellipsis.length;

    // 如果允许的基础名称长度太短，无法显示开头和结尾，则只显示结尾部分
    if (maxBaseLength < 2) {
         // 尽量显示扩展名前面的部分
        const keepEnd = Math.max(0, maxLength - ellipsis.length - extension.length);
        const truncatedBase = baseName.length > keepEnd ? baseName.slice(-keepEnd) : baseName;
        return ellipsis + truncatedBase + extension;
    }

    const keepStartCount = Math.ceil(maxBaseLength / 2);
    const keepEndCount = Math.floor(maxBaseLength / 2);

    const startPart = baseName.slice(0, keepStartCount);
    const endPart = baseName.slice(baseName.length - keepEndCount);

    return startPart + ellipsis + endPart + extension;
};

// 获取下次执行时间
const getNextExecutionTime = (cronExpression) => {
    if (!cronExpression) return '';
    
    try {
        // 分解cron表达式 "* * * * *" => [分钟, 小时, 日期, 月份, 星期]
        const parts = cronExpression.split(' ');
        if (parts.length !== 5) return '';
        
        const now = new Date();
        let next = new Date(now);
        
        // 简化的cron解析逻辑
        const cronMinute = parts[0];
        const cronHour = parts[1];
        const cronDayOfMonth = parts[2];
        const cronMonth = parts[3];
        const cronDayOfWeek = parts[4];
        
        // 设置下一分钟
        if (cronMinute === '*') {
            next.setMinutes(now.getMinutes() + 1);
            next.setSeconds(0);
            next.setMilliseconds(0);
        } else if (cronMinute.includes('/')) {
            // 每隔几分钟 (如: */5)
            const interval = parseInt(cronMinute.split('/')[1]);
            const currentMinute = now.getMinutes();
            const nextMinute = currentMinute + (interval - (currentMinute % interval || interval));
            next.setMinutes(nextMinute);
            next.setSeconds(0);
            next.setMilliseconds(0);
            if (nextMinute >= 60) {
                next.setMinutes(nextMinute % 60);
                next.setHours(now.getHours() + 1);
            }
        } else if (!isNaN(parseInt(cronMinute))) {
            // 指定分钟
            const specificMinute = parseInt(cronMinute);
            if (specificMinute <= now.getMinutes()) {
                next.setHours(now.getHours() + 1);
            }
            next.setMinutes(specificMinute);
            next.setSeconds(0);
            next.setMilliseconds(0);
        }
        
        // 设置下一小时 (简化逻辑)
        if (cronHour !== '*' && !cronHour.includes('/') && !isNaN(parseInt(cronHour))) {
            const specificHour = parseInt(cronHour);
            const currentHour = next.getHours();
            if (specificHour < currentHour || 
                (specificHour === currentHour && next.getMinutes() < now.getMinutes())) {
                next.setDate(next.getDate() + 1);
            }
            next.setHours(specificHour);
        }
        
        // 格式化时间 (使用-分隔符)
        const pad = (num) => num.toString().padStart(2, '0');
        const year = next.getFullYear();
        const month = pad(next.getMonth() + 1);
        const day = pad(next.getDate());
        const hour = pad(next.getHours());
        const minute = pad(next.getMinutes());
        const second = pad(next.getSeconds());
        
        return `${year}-${month}-${day} ${hour}:${minute}:${second}`;
    } catch (error) {
        console.error('解析Cron表达式错误:', error);
        return '';
    }
};


export { baseNotifyRT, baseNotifyInfo, getTimeY_d_m, truncateFileName, getNextExecutionTime};